package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.ser.impl.TypeWrappedSerializer;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.domain.*;
import com.zzyl.nursing.dto.CheckInApplyDto;
import com.zzyl.nursing.dto.CheckInConfigDto;
import com.zzyl.nursing.dto.CheckInElderDto;
import com.zzyl.nursing.dto.ElderFamilyDto;
import com.zzyl.nursing.mapper.*;
import com.zzyl.nursing.util.CodeGenerator;
import com.zzyl.nursing.vo.CheckInDetailVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.service.ICheckInService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * 入住信息管理Service业务层处理
 *
 * @author zzyl
 * @date 2024-12-06
 */
@Service

public class CheckInServiceImpl extends ServiceImpl<CheckInMapper, CheckIn> implements ICheckInService {
    @Autowired
    private CheckInMapper checkInMapper;

    /**
     * 查询入住信息管理
     *
     * @param id 入住信息管理主键
     * @return 入住信息管理
     */
    @Override
    public CheckIn selectCheckInById(Long id) {
        return getById(id);
    }

    /**
     * 查询入住信息管理列表
     *
     * @param checkIn 入住信息管理
     * @return 入住信息管理
     */
    @Override
    public List<CheckIn> selectCheckInList(CheckIn checkIn) {
        return checkInMapper.selectCheckInList(checkIn);
    }

    /**
     * 新增入住信息管理
     *
     * @param checkIn 入住信息管理
     * @return 结果
     */
    @Override
    public int insertCheckIn(CheckIn checkIn) {
        return save(checkIn) ? 1 : 0;
    }

    /**
     * 修改入住信息管理
     *
     * @param checkIn 入住信息管理
     * @return 结果
     */
    @Override
    public int updateCheckIn(CheckIn checkIn) {
        return updateById(checkIn) ? 1 : 0;
    }

    /**
     * 批量删除入住信息管理
     *
     * @param ids 需要删除的入住信息管理主键
     * @return 结果
     */
    @Override
    public int deleteCheckInByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除入住信息管理信息
     *
     * @param id 入住信息管理主键
     * @return 结果
     */
    @Override
    public int deleteCheckInById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    @Autowired
    private ElderMapper elderMapper;

    @Autowired
    private BedMapper bedMapper;
    @Autowired
    private CheckInConfigMapper checkInConfigMapper;

    @Override
    @Transactional
    public void apply(CheckInApplyDto dto) {
        LambdaQueryWrapper<Elder> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Elder::getIdCardNo, dto.getCheckInElderDto().getIdCardNo());
        queryWrapper.eq(Elder::getStatus, 1);
        Elder elder = elderMapper.selectOne(queryWrapper);
        if (ObjectUtils.isNotEmpty(elder)) {
            throw new BaseException("该老人已经入住");
        }

        Bed bed = bedMapper.selectBedById(dto.getCheckInConfigDto().getBedId());
        bed.setBedStatus(1);
        bedMapper.updateById(bed);

        elder = insertOrUpdateElder(bed, dto.getCheckInElderDto());

        String htnum = "LT" + CodeGenerator.generateContractNumber();

        //新增合同项目
        contract(htnum, elder, dto);

        //新增入住申请
        CheckIn checkIn = insertCheckIn(elder, dto, bed, dto.getElderFamilyDtoList());
        //新增入住配置
        insertCheckInConfig(checkIn.getId(), dto);

    }

    @Override
    public CheckInDetailVo detail(Long id) {
CheckIn checkIn = selectCheckInById(id);
CheckInDetailVo checkInDetailVo = new CheckInDetailVo();
BeanUtils.copyProperties(checkIn, checkInDetailVo);
return checkInDetailVo;

    }

    private void insertCheckInConfig(Long checkId, CheckInApplyDto dto) {
        CheckInConfig checkInConfig = new CheckInConfig();
        BeanUtils.copyProperties(dto.getCheckInConfigDto(), checkInConfig);
        checkInConfig.setCheckInId(checkId);
        checkInConfigMapper.insert(checkInConfig);
    }

    public CheckIn insertCheckIn(Elder elder, CheckInApplyDto dto, Bed bed, List<ElderFamilyDto> elderFamilyDtoList) {
        CheckIn checkIn = new CheckIn();
        BeanUtils.copyProperties(dto.getCheckInConfigDto(), checkIn);
        checkIn.setElderName(elder.getName());
        checkIn.setElderId(elder.getId());
        checkIn.setIdCardNo(elder.getIdCardNo());
        checkIn.setBedNumber(bed.getBedNumber());
        checkIn.setStatus(0);
        checkIn.setRemark(JSONUtil.toJsonStr(elderFamilyDtoList));
        save(checkIn);
        return checkIn;
    }

    @Autowired
    private ContractMapper contractMapper;

    public void contract(String contractNo, Elder elder, CheckInApplyDto checkInApplyDto) {
        Contract contract = new Contract();
        BeanUtils.copyProperties(checkInApplyDto.getCheckInContractDto(), contract);
        contract.setElderId(elder.getId());
        contract.setElderName(elder.getName());
        contract.setContractNumber(contractNo);
        LocalDateTime startDate = checkInApplyDto.getCheckInConfigDto().getStartDate();
        LocalDateTime endDate = checkInApplyDto.getCheckInConfigDto().getEndDate();
        Integer status = startDate.isBefore(LocalDateTime.now()) ? 0 : 1;
        contract.setStatus(status);
        contract.setStartDate(startDate);
        contract.setEndDate(endDate);
        contractMapper.insert(contract);


    }

    /**
     * 新增或者更新老人
     *
     * @param bed
     * @param dto
     * @return
     */

    public Elder insertOrUpdateElder(Bed bed, CheckInElderDto dto) {
        Elder elder = new Elder();

        BeanUtils.copyProperties(dto, elder);
        elder.setBedNumber(bed.getBedNumber());
        elder.setBedId(bed.getId());
        elder.setStatus(1);
        LambdaQueryWrapper<Elder> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Elder::getIdCardNo, dto.getIdCardNo()).ne(Elder::getStatus, 1);
        Elder elder1 = elderMapper.selectOne(queryWrapper);
        if (ObjectUtils.isNotEmpty(elder1)) {
            elderMapper.updateById(elder);
        } else {
            elderMapper.insert(elder);
        }
        return elder;
    }
}
